package itdfq.ydd.Behavior_Patters.zhongjiezhe;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @Author GocChin
 * @Date 2021/5/17 20:49
 * @Blog: itdfq.com
 * @QQ: 909256107
 * @Descript:
 */
public class Mediator {

    Map<String, Renter> m = new HashMap<>();
    Map<String, Saler> m2 = new HashMap<>();
    public void addRenter(Renter r) {
        m.put(r.getNO(), r);
    }
    public void addSaler(Saler s) {
        m2.put(s.getNO(), s);
    }

    public void send(Renter r, String msg) {
        System.out.println("come from renter-"+r.getNO()+"-"+r.getName());
        System.out.println("\t"+msg);
        Set<String> se = m2.keySet();
        Iterator<String> it = se.iterator();
        while(it.hasNext()) {
            String key = it.next();
            Saler sa = m2.get(key);
            sa.receive(r.getNO()+"-"+r.getName()+"-"+msg);
        }
    }

    public void send(Saler s, String msg) {
        System.out.println("come from saler-"+s.getNO()+"-"+s.getName());
        System.out.println("\t"+msg);
        Set<String> se = m.keySet();
        Iterator<String> it = se.iterator();
        while(it.hasNext()) {
            String key = it.next();
            Renter r = m.get(key);
            r.receive(s.getNO()+"-"+s.getName()+"-"+msg);
        }
    }

}
